#ifndef _CLUSTER_H_
#define _CLUSTER_H_

#include <vector>
#include "DocumentVector.h"
#include "Index.h"

class Cluster 
{

private:
	
	DocumentVector centroid;
	std::vector<DocumentVector> documents;
	Index index;
	
public:

	// Copy constructor
	Cluster(const Cluster& c) : centroid(c.centroid), documents(c.documents), index(c.index) {} 

	// Constructor that takes a DocumentVector which is the centroid
	Cluster(DocumentVector centroid_, Index index_) : centroid(centroid_), index(index_) {} 
	
	// Assignment operator
	Cluster& operator = (const Cluster& c)
	{
		if(this != &c) 
		{
			centroid = c.centroid;
			documents = c.documents;
			index = c.index;
		}
		
		return *this;
	}

	DocumentVector& get_centroid() 
	{
		return centroid;
	}
	
	std::vector<DocumentVector>& get_documents() 
	{
		return documents;
	}

	void add_document(DocumentVector& dv) 
	{
		documents.insert(documents.end(), dv);
	}

	void update_centroid()
	{
		int size = documents.size();

		if (size <= 1)
			return;


		double* sim = new double[size];

		for(int i = 0; i < size; i++)
		{
			sim[i] = 0;

			for(int j = 0; j < size; j++) 
			{
				index.similarity(documents[j], documents[i]);
				// sim[i] += documents[j].similarity(documents[i]);
			}
		}
		
		int max_index = 0;
		double max_sim = 0;

		for(int i = 0; i < size; i++) 
		{
			if(sim[i] > max_sim) 
			{
				max_sim = sim[i];
				max_index = i;
			}
		}

		centroid = documents[max_index];
		
		delete[] sim;
	}

	double similarity(DocumentVector& dv)
	{
		return index.similarity(centroid, dv);
		// return centroid.similarity(c.centroid);
	}
};
#endif /* _CLUSTER_H_ */
